import SM from 'sm-crypto'

const SM4 = SM.sm4
const encoder = new TextEncoder()

export function getBaseEnv() {
    const hostname = window.location.hostname
    let target = 'prod'
    if (hostname.includes('localhost') || hostname.includes('test-')) {
        target = 'dev'
    }

    return target
}

const SM4_DATA = {
    dev: ['024e60730bbb6bf6', '4492f2550d2ef12f'],
    prod: ['k5F9pL2qR8sT3wX7', 'mN4bV6cZ1xY9vU0o'],
}
const pwdKey = encoder.encode(SM4_DATA[getBaseEnv()]?.[0])
const iv = encoder.encode(SM4_DATA[getBaseEnv()]?.[1])

// console.log(11122, typeof iv, iv)
export function encrypt(text) {
    return SM4.encrypt(text, pwdKey, {
        mode: 'cbc', // 加密的方式有两种，ecb和cbc两种
        iv, // 初始向量，cbc模式的第二个参数，也需要跟后端配置的一致
    })
}
